import { Injectable, Logger } from "@nestjs/common";
import { loggingMiddleware, PrismaServiceOptions, QueryInfo } from "nestjs-prisma";

@Injectable()
class PrismaConfigService {
    constructor() {
        console.log("PrismaConfigService")
    }

    createPrismaOptions():PrismaServiceOptions | Promise<PrismaServiceOptions> {
        return {
            prismaOptions: {
                log: ["query", "info", "warn", "error"],
                transactionOptions: {
                    maxWait: 10 * 1000,
                    timeout: 20 * 1000
                }
            },
            // 项目启动就创建连接池，第一个查询立即响应，否则第一个个查询会出现延迟
            explicitConnect: true,
            middlewares: [
                loggingMiddleware({
                    logger: new Logger("PrismaMiddleware"),
                    logLevel: "log",
                    logMessage: (query:QueryInfo) => `[Prisma Query] ${query.model}.${query.action} - ${query.executionTime}ms`
                })
            ]
        }
    }
}

export default PrismaConfigService;